package com.lkk.cg.action;

import com.lkk.cg.base.BaseAction;
import com.lkk.cg.domain.Privilege;
import com.lkk.cg.domain.Role;
import com.lkk.cg.util.QueryHelper;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.HashSet;
import java.util.List;

/**
 * Created by lkk on 2014/12/1.
 */
@Controller
@Scope("prototype")
public class RoleManageAction extends BaseAction<Role> {

    Long[] privilegeIds;

    /**
     * 列表
     *
     * @return
     */
    public String list() {
        QueryHelper queryHelper = new QueryHelper(Role.class, "r");
        queryHelper.preparePageBean(roleService, getPageNum(), getPageSize());
        return "list";
    }

    /**
     * 删除
     *
     * @return
     */
    public String delete() {
        Role role = roleService.getById(model.getId());
        if (role == null)
            addActionError("要删除的角色不存在！");
        else {
            if (role.getIsDefault())
                addActionError("不能删除默认角色！");
            else
                roleService.delete(model.getId());
        }
        return "toList";
    }

    /**
     * 添加页面
     *
     * @return
     */
    public String addUI() {
        return "saveUI";
    }

    /**
     * 添加
     *
     * @return
     */
    @InputConfig(resultName = "saveUI")
    public String add() {
        Role role = new Role();
        role.setName(model.getName());
        role.setDescription(model.getDescription());
        roleService.save(role);
        return "toList";
    }

    /**
     * 添加页面
     *
     * @return
     */
    public String editUI() {
        Role role = roleService.getById(model.getId());
        ActionContext.getContext().getValueStack().push(role);
        return "saveUI";
    }

    /**
     * 添加
     *
     * @return
     */
    public String edit() {
        Role role = roleService.getById(model.getId());
        role.setName(model.getName());
        role.setDescription(model.getDescription());
        roleService.update(role);
        return "toList";
    }

    /**
     * 设置权限页面
     *
     * @return
     */
    public String setPrivilegeUI() {

        List<Privilege> privilegeList = privilegeService.getAll();
        Role role = roleService.getById(model.getId());

        //准备数据
        ActionContext.getContext().put("privilegeList", privilegeList);
        ActionContext.getContext().getValueStack().push(role);

        //准备回显
        privilegeIds = new Long[role.getPrivileges().size()];
        int i = 0;
        for (Privilege p : role.getPrivileges()) {
            privilegeIds[i] = p.getId();
            i++;
        }

        return "setPrivilegeUI";
    }

    /**
     * 设置权限
     *
     * @return
     */
    public String setPrivilege() {

        List<Privilege> privilegeList = privilegeService.getByIds(privilegeIds);
        Role role = roleService.getById(model.getId());
        //todo 可能会导致不会保存的bug
        role.setPrivileges(new HashSet<Privilege>(privilegeList));
        roleService.update(role);

        return "toList";
    }

    public String setDefaultRole() {
        roleService.setDefaultRole(model.getId());
        addActionMessage("默认角色修改成功");
        return "toList";
    }

    public Long[] getPrivilegeIds() {
        return privilegeIds;
    }

    public void setPrivilegeIds(Long[] privilegeIds) {
        this.privilegeIds = privilegeIds;
    }
}
